{extend name="common/layout" /}

{block name="body"}
<div class="container">
    <!-- 表格栏 -->
    <div class="layui-card">
        <div class="layui-card-header">导航菜单</div>
        <div class="layui-card-body">
            <table id="wait-table-list" lay-filter="wait-table-list"></table>
            <script type="text/html" id="toolbar">
                <div class="layui-btn-container">
                    <a class="layui-btn layui-btn-sm layui-btn-default {:check_perms('add')}" lay-event="add">
                        <i class="layui-icon icon-add"></i>
                        <span>新增</span>
                    </a>
                </div>
            </script>
            <script type="text/html" id="table-disable">
                {{#  if(d.is_disable === 0){ }}
                    <span class="color-success"><i class="layui-icon layui-icon-circle-dot" lay-tips="正常"></i></span>
                {{# } else { }}
                    <span class="color-warning"><i class="layui-icon layui-icon-circle-dot" lay-tips="禁用"></i></span>
                {{#  } }}
            </script>
            <script type="text/html" id="table-operate">
                <button type="button" class="layui-btn layui-btn-xs layui-btn-default {:check_perms('edit')}" lay-event="edit">
                    <i class="layui-icon icon-edit"></i>
                </button>
                <button type="button" class="layui-btn layui-btn-xs layui-btn-danger {:check_perms('del')}" lay-event="del">
                    <i class="layui-icon icon-del"></i>
                </button>
            </script>
        </div>
    </div>
</div>
{/block}

{block name="js"}
<script>
    layui.use(['treeTable'], function () {
        let treeTable = layui.treeTable;

        // 渲染表格
        let table = layui.treeTable.render({
            tree: {
                customName: {
                    children: 'children'
                    ,name: 'name'
                    ,pid: 'pid'
                    ,id: 'id'
                }
                ,view: {
                    showIcon: false
                    ,showFlexIconIfNotParent: false
                }
                ,data: {
                    rootPid: 0
                    ,isSimpleData: true
                }
            }
            ,id: 'wait-table-list'
            ,elem: '#wait-table-list'
            ,url: '{:route("setting.pc.navigation/index")}'
            ,toolbar: '#toolbar'
            ,defaultToolbar: ['filter', 'exports', 'print']
            ,skin: 'line'
            ,cols: [[
                {field:'id', title:'ID', width:70, align:'center'},
                {field:'name', title:'名称', minWidth:200},
                {field:'target', title:'跳转', minWidth:80},
                {field:'url', title:'链接', minWidth:300},
                {field:'sort', title:'排序', minWidth:70, align:'center'},
                {field:'is_disable', title:'状态', minWidth:60, align:'center', templet:'#table-disable'},
                {field:'right', title:'操作', width:90, align:'center', toolbar:'#table-operate'},
            ]]
        });

        // 逻辑事件
        waitUtil.event({
            add: function () {
                waitUtil.popup({
                    title: '新增',
                    url: '{:route("setting.pc.navigation/add")}',
                    area: ['500px', '470px'],
                    success: function (layero, index) {
                        layero.layui.form.on('submit(addForm)', function(data) {
                            waitUtil.locking(this);
                            waitUtil.ajax({
                                url: '{:route("setting.pc.navigation/add")}',
                                type: 'POST',
                                data: data.field
                            }).then((res) => {
                                waitUtil.unlock(this);
                                if (res.code === 0) {
                                    table.reload();
                                    layer.close(index);
                                }
                            });
                        });
                    }
                });
            },
            edit: function (obj) {
                waitUtil.popup({
                    title: '编辑',
                    url: '{:route("setting.pc.navigation/edit")}?id='+obj.data.id,
                    area: ['500px', '470px'],
                    success: function (layero, index) {
                        layero.layui.form.on('submit(addForm)', function(data) {
                            waitUtil.locking(this);
                            data.field['id'] = obj.data.id;
                            waitUtil.ajax({
                                url: '{:route("setting.pc.navigation/edit")}',
                                type: 'POST',
                                data: data.field
                            }).then((res) => {
                                waitUtil.unlock(this);
                                if (res.code === 0) {
                                    table.reload({page: {curr: 1}});
                                    layer.close(index);
                                }
                            });
                        });
                    }
                });
            },
            del: function (obj) {
                layer.confirm('确定要删除此项数据吗？', function(index) {
                    layer.close(index);
                    waitUtil.ajax({
                        url: '{:route("setting.pc.navigation/del")}',
                        type: 'POST',
                        data: {id: obj.data.id}
                    }).then((res) => {
                        if (res.code === 0) {
                            table.reload();
                            layer.msg(res.msg, {icon: 1, time: 1000});
                        }
                    });
                });
            },
            expand: function () {
                treeTable.expandAll('wait-table-list', true)
            },
            collapse: function () {
                treeTable.expandAll('wait-table-list', true)
            }
        });
    });
</script>
{/block}